export const parseTable = async function(biffer, Item, wog, idSub) {
	const [count] = biffer.unpack('<l');

	const items = [];

	for(let i = 1; i <= count; i++) {
		if(i % 1000 == 0 || i == count || i == 1) {
			wog.set(idSub, wog.wait, `(${i}/${count}) 解析中...`);
		}

		const pos = biffer.tell();
		const [offset] = biffer.unpack('<l');

		biffer.seek(pos + offset);

		items.push(await Item.parse(biffer, i));

		biffer.seek(pos + 4);
	}

	wog.set(idSub, wog.done, `完成，共：${count}`);

	return items;
};

export const parseTableEntry = function(biffer, typesKey) {
	const result = {};

	const posEntry = biffer.tell();

	const posKey = posEntry - biffer.unpack('<l')[0];
	biffer.seek(posKey);

	biffer.unpack(`<${typesKey.length}H`).forEach((offset, i) => {
		const entry = typesKey[i];

		if(entry == null) { return; }

		const [key, type] = entry;
		let value = null;

		if(!offset || !type) {
			value = null;
		}
		else {
			let pos = posEntry + offset;
			biffer.seek(pos);

			if(type == 'offset') {
				value = pos;
			}
			else if(type == 'string') {
				let [offsetString] = biffer.unpack('<l');

				biffer.skip(offsetString - 4);
				value = biffer.unpackString();
			}
			else {
				[value] = biffer.unpack(type);
			}
		}

		result[key] = value;
	});

	return result;
};